package action;

import config.ApplicationConfig;
import server.LoginServer;
import server.SettingServer;
import server.UserServer;
import util.UploadFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedHashMap;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2019-04-14 Sunday 15:24:47
 */
public class ChangeUserIconServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
        request.setCharacterEncoding(pageConfig.get("encode"));
        response.setCharacterEncoding(pageConfig.get("encode"));

        HttpSession session = request.getSession();
        String username = session.getAttribute("user") == null ? "" : session.getAttribute("user").toString();
        String cookie = session.getAttribute("cookie") == null ? "" : session.getAttribute("cookie").toString();
        String page = "information.jsp";
        if (username == null || username.equals("") || cookie == null || cookie.equals("")) {
            session.setAttribute("infoTitle", "未登录");
            session.setAttribute("infoContent", "您还未登录，请先登录");
            session.setAttribute("return", "index.jsp");
            session.setAttribute("panelColor", "red");
            session.removeAttribute("user");
            session.removeAttribute("cookie");
        } else {
            LoginServer loginServer = new LoginServer();
            loginServer.open();
            if (!loginServer.checkLogin(username, cookie)) {
                UploadFile uploadFile = new UploadFile();
                String[] uuid = uploadFile.getUploadFile(getServletConfig(), request, response, "jpeg,jpg,png");
                if (uuid.length > 0 && uuid[0] != null && !uuid[0].equals("")) {
                    UserServer userServer = new UserServer();
                    userServer.open();
                    userServer.changeIcon(username, uuid[0]);
                    userServer.close();
                    session.setAttribute("infoTitle", "修改头像");
                    session.setAttribute("infoContent", "修改成功");
                    session.setAttribute("return", "mine");
                    session.setAttribute("panelColor", "blue");
                } else {
                    session.setAttribute("infoTitle", "修改头像");
                    session.setAttribute("infoContent", "修改失败，请上传jpg或jpeg或png图像文件");
                    session.setAttribute("return", "mine");
                    session.setAttribute("panelColor", "red");
                }
            } else {
                session.setAttribute("infoTitle", "未登录");
                session.setAttribute("infoContent", "您还未登录，请先登录");
                session.setAttribute("return", "index.jsp");
                session.setAttribute("panelColor", "red");
                session.removeAttribute("user");
                session.removeAttribute("cookie");
            }
            loginServer.close();
        }
        response.sendRedirect(page);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
